package com.dhcc.bpm.modules.log.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dhcc.bpm.common.json.JSONObject;
import com.dhcc.bpm.modules.log.entity.BasePrdtBusiness;
import com.dhcc.bpm.modules.log.service.IBasePrdtBusinessService;
import com.dhcc.bpm.modules.log.vo.BasePrdtBusinessVO;
import com.dhcc.bpm.modules.log.vo.BusinessVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * 基础产品业务服务地址表 控制器
 *
 * @author xiafayu
 * @since 2021-06-23
 */
@RestController
@AllArgsConstructor
@RequestMapping("bpm-system/baseprdtbusiness")
@Api(value = "基础产品业务服务地址表", tags = "基础产品业务服务地址表接口")
public class BasePrdtBusinessController extends BladeController {
	private final static Logger log = LoggerFactory.getLogger(BasePrdtBusinessController.class);

	private IBasePrdtBusinessService basePrdtBusinessService;
	@Resource(name = "loadBalanced")
	private RestTemplate loadBalancedRestTemplate;
	@Resource(name = "restTemplate")
	private RestTemplate restTemplate;
	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入basePrdtBusiness")
	public R<BasePrdtBusiness> detail(BasePrdtBusiness basePrdtBusiness) {
		BasePrdtBusiness detail = basePrdtBusinessService.getOne(Condition.getQueryWrapper(basePrdtBusiness));
		return R.data(detail);
	}

	/**
	 * 分页 基础产品业务服务地址表
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入basePrdtBusiness")
	public R<IPage<BasePrdtBusiness>> list(BasePrdtBusiness basePrdtBusiness, Query query) {
		IPage<BasePrdtBusiness> pages = basePrdtBusinessService.page(Condition.getPage(query), Condition.getQueryWrapper(basePrdtBusiness));
		return R.data(pages);
	}

	/**
	 * 自定义分页 基础产品业务服务地址表
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入basePrdtBusiness")
	public R<IPage<BasePrdtBusinessVO>> page(BasePrdtBusinessVO basePrdtBusiness, Query query) {
		IPage<BasePrdtBusinessVO> pages = basePrdtBusinessService.selectBasePrdtBusinessPage(Condition.getPage(query), basePrdtBusiness);
		return R.data(pages);
	}

	/**
	 * 新增 基础产品业务服务地址表
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入basePrdtBusiness")
	public R save(@Valid @RequestBody BasePrdtBusiness basePrdtBusiness) {
		return R.status(basePrdtBusinessService.save(basePrdtBusiness));
	}

	/**
	 * 修改 基础产品业务服务地址表
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入basePrdtBusiness")
	public R update(@Valid @RequestBody BasePrdtBusiness basePrdtBusiness) {
		return R.status(basePrdtBusinessService.updateById(basePrdtBusiness));
	}

	/**
	 * 新增或修改 基础产品业务服务地址表
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入basePrdtBusiness")
	public R submit(@Valid @RequestBody BasePrdtBusiness basePrdtBusiness) {
		return R.status(basePrdtBusinessService.saveOrUpdate(basePrdtBusiness));
	}

	
	/**
	 * 删除 基础产品业务服务地址表
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 8)
	@ApiOperation(value = "删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(basePrdtBusinessService.removeByIds(Func.toLongList(ids)));
	}

	/**
	 * 查询业务流水数据
	 */
	@PostMapping("/getBusinessData")
	@ApiOperationSupport(order = 8)
	@ApiOperation(value = "查询业务流水数据", notes = "传入")
	public R getBusinessData(@Valid @RequestBody BusinessVO business) {
		try {
			BasePrdtBusinessVO basePrdtBusinessVO = basePrdtBusinessService.getOneByProductCode(business.getProductCode());
			if (basePrdtBusinessVO != null) {
				String address = basePrdtBusinessVO.getServerAddress();
				ResponseEntity<JSONObject> result = null;
				if ("1".equals(basePrdtBusinessVO.getType())) {
					//内部注册中心服务名调用
					result = loadBalancedRestTemplate.postForEntity(address, business, JSONObject.class);
				} else {
					//外部ip端口调用
					result = restTemplate.postForEntity(address, business, JSONObject.class);
				}
				if (HttpStatus.OK.equals(result.getStatusCode())) {
					return R.data(result.getBody());
				}
			}
		}catch (Exception e){
			log.error("获取业务数据异常:{}", e);
			return R.data(null);
		}
		return R.data(null);
	}

}
